package com.hhit.collaborative_learning.learning;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import com.hhit.collaborative_learning.dao.impl.PublicDaoImplMySql;
import com.mysql.jdbc.StringUtils;

public class SaveBlogServlet extends HttpServlet {
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String blogName = request.getParameter("titl");
		String type = request.getParameter("typ");
		String blogContent = request.getParameter("cont");
		String blogDesc = request.getParameter("desc");
		String category = request.getParameter("chnl");
		String canComment = request.getParameter("comm");
		String isTop = request.getParameter("top");
		String tag = request.getParameter("tag2");
		String blogId = request.getParameter("artid");
		String userId = request.getParameter("userId");
		String isauto = request.getParameter("isauto");
		String isDelete = request.getParameter("stat");
//		String xh=request.getParameter("xh");
//		System.out.println("xh="+xh);
//		System.out.println("**************************************************");
//		System.out.println("con="+blogContent);
		isauto=StringUtils.isNullOrEmpty(isauto)?"":isauto;
//		System.out.println("blogId="+blogId);
//		System.out.println("blogName="+blogName);
		JSONObject json = new JSONObject();
		PublicDaoImplMySql dao = new PublicDaoImplMySql();
		String result="";
		if (StringUtils.isNullOrEmpty(blogId) || blogId.equals("0")) {
			result += dao.saveBlog(true, blogName, blogContent, blogDesc,
					category, canComment, isTop, tag, userId, isDelete, blogId,
					type);
		}else{
			result += dao.saveBlog(false, blogName, blogContent, blogDesc,
					category, canComment, isTop, tag, userId, isDelete, blogId,
					type);
		}
		if(isauto.equals("1")||isDelete.equals("2")){
			if (result.startsWith("true")) {
				json.put("result", 1);
				json.put("data", blogId.equals("0")?result.substring(5):blogId);
			} else {
				json.put("result", 0);
			}
			response.getWriter().print(json.toString());
		}else if(isDelete.equals("0")){
			if (result.startsWith("true")) {
				json.put("result", 1);
				json.put("content", "文章列表");
				json.put("data", request.getScheme() + "://"
						+ request.getServerName() + ":" + request.getServerPort()
						+ request.getContextPath() + "/"+"learning/bloglist.jsp");
			} else {
				json.put("result", 0);
			}
			response.getWriter().print(json.toString());
		}
	}

}
